local skel = fk.CreateSkill {
  name = "rmt__yuling",
}

skel:addEffect(fk.RoundEnd, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local room = player.room
      local targets = {}
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data
        if use.card.trueName == "slash" then
          table.insert(targets, use.from)
          return true
        end
      end, Player.HistoryRound)
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId, true).trueName == "slash" then
                table.insert(targets, move.from)
                return true
              end
            end
          end
        end
      end, Player.HistoryRound)
      room.logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local use = e.data
        if use.card.trueName == "slash" then
          table.insert(targets, use.from)
          return true
        end
      end, room.logic:getCurrentEvent().id)
      targets = table.filter(targets, function (to) return not to.dead end)
      if #targets > 0 then
        room:sortByAction(targets)
        event:setCostData(self, {tos = targets})
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    for _, to in ipairs(tos) do
      room:addPlayerMark(to, "@rmt__yuling")
    end
    local invoke = room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#rmt__yuling-invoke"})
    for _, to in ipairs(tos) do
      room:setPlayerMark(to, "@rmt__yuling", 0)
    end
    return invoke
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    local me = table.contains(tos, player) and player
    for _, to in ipairs(tos) do
      if not to.dead and not to:isNude() then
        if me and not me.dead and me ~= to then
          local cid = room:askToChooseCard(me, {target = to, flag = "he", skill_name =  skel.name})
          room:askToYiji(me, {
            cards = {cid}, targets = room:getOtherPlayers(to), skill_name = skel.name,
            min_num = 1, max_num = 1,
            prompt = "#rmt__yuling-card:"..to.id,
            expand_pile = {cid},
          })
        else
          room:askToYiji(to, {
            cards = to:getCardIds("he"), targets = room:getOtherPlayers(to), skill_name = skel.name,
            min_num = 1, max_num = 1,
            prompt = "#rmt__yuling-card:"..to.id,
          })
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__yuling"] = "御凌",
  [":rmt__yuling"] = "每轮结束时，你可以令本轮最先使用【杀】、最先弃置【杀】、最后使用【杀】的角色依次分配一张牌。若含你，均由你分配。",
  ["#rmt__yuling-invoke"] = "御凌：是否令满足条件的角色各分配一张牌？",
  ["#rmt__yuling-card"] = "御凌：请分配 %src 的一张牌",
  ["@rmt__yuling"] = "御凌分配",

  ["$rmt__yuling1"] = "御国之术者，政出于一。",
  ["$rmt__yuling2"] = "天命有常者，在予一人。",
}

return skel
